# Create your views here.
import urllib2, time
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from spojstats.main.models import *

def update_user_history(req, username):
	url = urllib2.urlopen('http://www.spoj.pl/status/'+username+'/signedlist/')
	data = url.read()
	data = data.split('\n')
	#data = open('input.txt').readlines()
	for line in data[9:]:
		no_of_entries = 0
		if line.startswith('|'): 
			line = line.split("|")
			line = [ x.strip() for x in line ]
			userentry = User(
									id = line[1],
									submit_date = line[2],
									prob   = line[3],
									result = line[4],
									mem    = line[6],
									lang   = line[7],
									username = username
								)
			userentry.put()
			no_of_entries += 1
	
	return no_of_entries
	
def index(request):
	t1 = time.clock()
	t2 = time.clock()
	return render_to_response('index.html', {'time' : t2 - t1})
	
def clean_db(request):
	#clean the database!!!
	t1 = time.clock()
	q = User.all()
	results = q.fetch(1000)
	while len(results) != 0:
		db.delete(results)
		results = q.fetch(1000)
	t2 = time.clock()
	return render_to_response('cleandb.html', { 'time' : t2 - t1 })
	
def user_profile(req):
	t1 = time.clock()
	if req.method == 'POST' and req.POST['username']:
		username = req.POST['username']
		entries = list(User.gql("WHERE username = :1 ORDER BY id DESC", username))
		if not entries:
			update_user_history(req, username)
			entries = list(User.gql("WHERE username = :1 ORDER BY id DESC", username))
		no_of_entries = len(entries)
	else:
		username = ""
		entries = []
		no_of_entries = 0
	
	t2 = time.clock()
	return render_to_response('user.html', { 'username' : username, 'entries' : entries,
											 'no_of_entries' : no_of_entries, 'time' : t2 - t1 })
	
def user_profile_detail(req, username='', lang=''):
	t1 = time.clock()
	if lang == '':
		entries = list(User.gql("WHERE username = :1 ORDER BY id DESC", username))
		if not entries:
			update_user_history(req, username)
			entries = list(User.gql("WHERE username = :1 ORDER BY id DESC", username))
		no_of_entries = len(entries)
	else:
		entries = list(User.gql("WHERE username = :1 AND lang = :2 ORDER BY id DESC", username, lang))
		if not entries:
			update_user_history(req, username)
			entries = list(User.gql("WHERE username = :1 AND lang = :2 ORDER BY id DESC", username, lang))
		no_of_entries = len(entries)
	
	t2 = time.clock()
	return render_to_response('user.html', { 'username' : username, 'entries' : entries,
											 'no_of_entries' : no_of_entries, 'time' : t2 - t1 })
											 
def problem(request):
	t1 = time.clock()
	no_of_entries = ''
	problem_code = ''
	t2 = time.clock()
	return render_to_response('problem.html', { 'problem' : problem_code, 'no_of_entries' : no_of_entries, 'time' : t2 - t1 })
	
def problem_details(request, problem_code=''):
	t1 = time.clock()
	entries = list(User.gql("WHERE prob =:1", problem_code))
	t2 = time.clock()
	return render_to_response('problem.html', { 'entries' : entries, 'problem_code' : problem_code, 'no_of_entries' : len(entries), 'time' : t2 - t1 })
	